package d5_proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.nio.file.attribute.UserPrincipalLookupService;

public class ProxyUtil {
    public static UserService createProxy(UserServiceimpl userServiceimpl){
        UserService userServiceProxy =  (UserService) Proxy.newProxyInstance(d4_proxy.ProxyUtil.class.getClassLoader(),new Class[]{UserService.class},new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if(method.getName().equals("login")||method.getName().equals("logout")||method.getName().equals("isLogin")){
                    long startTime = System.currentTimeMillis();
                    Object result = method.invoke(userServiceimpl,args);
                    long endTime = System.currentTimeMillis();
                    System.out.println((endTime - startTime)/1000 + "s");
                    return result;
                }else {
                    Object result = method.invoke(userServiceimpl,args);
                    return result;
                }

            }
        });
        return userServiceProxy;
    }
}
